/*
 * @Author: xuxu12138 1718324422@qq.com
 * @Date: 2023-08-29 08:44:40
 * @LastEditors: xuxu12138 1718324422@qq.com
 * @LastEditTime: 2024-01-29 09:22:45
 * @FilePath: /things-admin/src/view/news/FormBloc.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import {getNews, NewsVM, postNews, putNews} from '@/base/api/NewsApi';
import {News} from '@/base/model/News';
class Form {
  id = 0;
  title = '';
  newsType='PUSH_INFORM';
  images: Array<string> = [];
  body = '';
  targetGroup = 'ALL_MEMBER';
  pushTime = '';
  pushType = '';
  actions='OPEN_URL';
  actionData='';
  targetAccounts: Array<string> = [];
  targetAccountsTxt = '';
  type = 0;
}
export class FormBloc {
  form = new Form();

  load(id: number) {
    return getNews(id)
      .then((res) => {
        this.form.id = res.data.id;
        this.form.actions = res.data.actions;
        this.form.body = res.data.body;
        this.form.newsType = res.data.newsType;
        this.form.type = res.data.type;
        this.form.pushType = res.data.pushType;
        this.form.targetGroup = res.data.targetGroup;
        this.form.title = res.data.title;
        this.form.actionData = res.data.actionData;
        this.form.images = res.data.images;
        this.form.pushTime = res.data.pushTime;
        this.form.targetAccounts = res.data.targetAccounts;
      });
  }

  isDetail = false;

  save(id: number | '') {
    const entity: NewsVM = {
      actions: this.form.actions,
      body: this.form.body,
      newsType: this.form.newsType,
      pushType: this.form.pushType,
      targetGroup: this.form.targetGroup,
      title: this.form.title,
      actionData: this.form.actionData,
      images: this.form.images,
      type: this.form.type,
      pushTime: this.form.pushTime,
      targetAccounts: this.form.targetAccounts
    };
    return id ? putNews(id, entity) : postNews(entity);
  }
}
